﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Data;
namespace ProjectSem40701.Util
{
    public class CheckValidUser
    {
        //khai bao mot so hang so ve trang thai cua user
        private const int Unknow_Error = 0;
        private const int Exist_In_User_Round_But_InActive = 1;
        private const int Not_Exist_In_User_Round = 2;
        private const int Exist_In_User_Round_AndActive = 3;

        //check xem user co duoc phep vao thi hay khong
        public int IsValidUser()
        {
            Util.CheckValidUser objCheckValidUser = new Util.CheckValidUser();
            int ok = Unknow_Error;
            try
            {
                string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                Guid userID = new Guid(Membership.GetUser(strUser).ProviderUserKey.ToString());
                //int rid = Convert.ToInt32(Session["roundID"].ToString());
                GetActiveRoundID objGetActiveRoundID = new GetActiveRoundID();
                GetActiveExamID objGetActiveExamID = new GetActiveExamID();
                int examID = objGetActiveExamID.ExamID();
                int rid = objGetActiveRoundID.RoundID(examID);

                MainDataset.UserRoundTableAdapters.tbl_UserRound_Select_All_AllowTest_By_UserIDTableAdapter dp =
                    new MainDataset.UserRoundTableAdapters.tbl_UserRound_Select_All_AllowTest_By_UserIDTableAdapter();
                DataTable dttblUserRound = dp.Get_tbl_UserRound_Select_All_By_UserID_RoundID(userID, rid);

                //Neu ton tai user id va roundid trong bang userround
                if (dttblUserRound.Rows.Count > 0)
                {
                    if (Convert.ToInt32(dttblUserRound.Rows[0]["Status"].ToString()) == 1)
                    {
                        //ton tai trong bang userround va trang thai duoc kich hoat ==> duoc phep vao thi
                        ok = Exist_In_User_Round_AndActive;
                    }
                    else
                    {
                        //Ko ton tai trong bang userround va trang thai duoc kich hoat ==> Ko duoc phep vao thi
                        ok = Exist_In_User_Round_But_InActive;
                    }
                }
                //user va roundid ko ton tai trong bang userround
                else
                {
                    ok = Not_Exist_In_User_Round;
                }

            }
            catch
            {
                ok = Unknow_Error;
            }
            return ok;
        }
    }
}
